$(window).load(function () {
	$(window).resize(function(){
		addControlZoombar();
	});

	$("#buildingInfo").hide();
	$("#publicOfficialBuildingInfo").hide();


	var map = getInitializedMap(lonlat);

	var wms_layers = map.getLayersByClass('OpenLayers.Layer.WMS');
	var wms = wms_layers[0];

	var wmsRequestStringOptions = {
	        REQUEST: "GetFeatureInfo",
	        EXCEPTIONS: "application/vnd.ogc.se_xml",
	        BBOX: wms.map.getExtent().toBBOX(),
	        X: 0,
	        Y: 0,
			INFO_FORMAT: 'text/plain', // 'text/plain', 'application/vnd.ogc.gml',
	        QUERY_LAYERS: 'umanji:building', // wms.params.LAYERS, 다 가져올 경우
	        WIDTH: wms.map.size.w,
	        HEIGHT: wms.map.size.h,
	        FORMAT: 'rss'}

    var building_mgt_sn_pattern=/\s(\d){25}\s/g;
	var orgAtrrPOBlHref = $("#poBuildingBoardLink").attr('href');
	var orgAtrrUlHref = $("#updateLink").attr('href');
	var orgAtrrClHref = $("#createEnterpriseLink").attr('href');

	// 드래그 레이어 등록 및 이벤트 처리 
    var dragFeature = new OpenLayers.Control.DragFeature(layer);
    dragFeature.onComplete = function(layer, pixel) {
    	lonlat = map.getLonLatFromViewPortPx(pixel);

        wmsRequestStringOptions['BBOX'] = wms.map.getExtent().toBBOX();
        wmsRequestStringOptions['WIDTH'] = wms.map.size.w;
        wmsRequestStringOptions['HEIGHT'] = wms.map.size.h;
    	wmsRequestStringOptions['X'] = pixel.x;
        wmsRequestStringOptions['Y'] = pixel.y;
        var url =  wms.getFullRequestString(wmsRequestStringOptions);

        $.get(contextRootURL + 'umj/util/getWmsQueryPage?' + url.split("?")[1], function(data) {
    		var building_mgt_sn = data.match(building_mgt_sn_pattern);
    		if (building_mgt_sn) {
        		var building_no = $.trim(new String(building_mgt_sn));
    			$.getJSON('pob/' + building_no, function(data) {
					$.getJSON('info/' + building_no, function(data) {
						$("#address").text(data['buildingAddress']);
						$("#poBuildingBoardLink").attr('href', orgAtrrPOBlHref + building_no);
						$("#updateLink").attr('href', orgAtrrUlHref + building_no);
						$("#createEnterpriseLink").attr('href', orgAtrrClHref + building_no);						
					});

		    		var popup = new OpenLayers.Popup.FramedCloud("umanji", map.getLonLatFromPixel(pixel), null, 
																	'이 지점에 등록합니다.', null, true );
		    		popup.panMapIfOutOfView = true;
		    		popup.closeOnMove = true;
		    		map.addPopup(popup, true);

		    		layer.removeFeatures(layer.features);
		    		
		    		layer.addFeatures( [ new OpenLayers.Feature.Vector (
		    		    new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
		    		) ] );
		    		
    				if (data == true) {
    					alert("data : "+ data);
   						$("#publicOfficialBuildingInfo").show("slow");
   						$("#buildingInfo").hide();
   						$("#selectMapMsg").hide();
    				} else {
   						$("#buildingInfo").show("slow");
   						$("#publicOfficialBuildingInfo").hide();
   						$("#selectMapMsg").hide();
    				}
    			});
    		} else {
    			
    		}
		});

    };
	map.addControl(dragFeature);
    dragFeature.activate();

	// addControlZoombar();

	// 마우스 클릭 시 발생 이벤트 처리
    map.events.register('click', map, function (e) {
    	lonlat = map.getLonLatFromViewPortPx(e.xy);

        OpenLayers.Util.getElement('nodeList').innerHTML = "Loading... please wait...";

        wmsRequestStringOptions['BBOX'] = wms.map.getExtent().toBBOX();
        wmsRequestStringOptions['WIDTH'] = wms.map.size.w;
        wmsRequestStringOptions['HEIGHT'] = wms.map.size.h;
        wmsRequestStringOptions['X'] = e.xy.x;
        wmsRequestStringOptions['Y'] = e.xy.y;
        var url =  wms.getFullRequestString(wmsRequestStringOptions);
    	// cross domain 문제로 인해 YQL로 대체함 2010년 4월 16일
    	// OpenLayers.loadURL(url, '', this, setHTML);

        $.get(contextRootURL + 'umj/util/getWmsQueryPage?' + url.split("?")[1], function(data) {
    		var building_mgt_sn = data.match(building_mgt_sn_pattern);
    		if (building_mgt_sn) {
        		var building_no = $.trim(new String(building_mgt_sn));
    			$.getJSON('pob/' + building_no, function(data) {
					$.getJSON('info/' + building_no, function(data) {
						$("#address").text(data['buildingAddress']);
						$("#poBuildingBoardLink").attr('href', orgAtrrPOBlHref + building_no);
						$("#updateLink").attr('href', orgAtrrUlHref + building_no);
						$("#createEnterpriseLink").attr('href', orgAtrrClHref + building_no);
					});

		    		var popup = new OpenLayers.Popup.FramedCloud("umanji", map.getLonLatFromPixel(e.xy), null, 
																	'이 지점에 등록합니다.', null, true );
		    		popup.panMapIfOutOfView = true;
		    		popup.closeOnMove = true;
		    		map.addPopup(popup, true);

		    		layer.removeFeatures(layer.features);
		    		
		    		layer.addFeatures( [ new OpenLayers.Feature.Vector (
		    		    new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
		    		) ] );
		    		
    				if (data == true) {
    					alert("data : "+ data);
   						$("#publicOfficialBuildingInfo").show("slow");
   						$("#buildingInfo").hide();
    				} else {
   						$("#buildingInfo").show("slow");
   						$("#publicOfficialBuildingInfo").hide();
    				}
					$("#selectMapMsg").hide();
    			});
    		} else {
    			
    		}
		});

        OpenLayers.Event.stop(e);
    });
});
// BD_MGT_SN = 1144011000100400051020214 RD_MGT_SN = P11440002497 EQB_MGT_SN = null BD_NM = 박여인숙 BD_ENG_NM = null BD_STS_CM = null BD_KD = 06999 BD_IN_SE = M PRECISE_SE = 3 DET_BD_NM = null ZIP_CL = 121-806 MT_YN = 0 UP_YMD = 20080806 WRK_MGT_SN = A11440080703000123 ANN_YN = 1 ANN_YMD = 20080303 ANN_DE = null NOTICE_YMD = null RESULT_SE = null ANN_SE = null EMD_CD = 1144011000 UDRT_YN = 0 BD_MA_SN = 34 BD_SB_SN = 0 LNDN_MA_SN = 40 LNDN_SB_SN = 51 ABV_FL_CNT = 2 UDR_FL_CNT = 0 RN_MGT_SN = null BD_MK_YMD = null UPDATE_YMD = null TRS_YMD = null the_geom = [GEOMETRY (MultiPolygon) with 5 points] CODE = null 